package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FrontController extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public FrontController() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String requestType = request.getParameter("requestType");
		if (requestType.equals("Confirmation")) {
			request.setAttribute("email", request.getParameter("email"));
			request.setAttribute("token", request.getParameter("token"));

		} else if (requestType.equals("CreateGroup")) {
			request.setAttribute("email", session.getAttribute("email"));
			request.setAttribute("name", request.getParameter("name"));

		} else if (requestType.equals("Delete")) {
			request.getRequestDispatcher(requestType)
					.forward(request, response);
		} else if (requestType.equals("DeleteGroup")) {
			request.setAttribute("groups", request.getParameter("groups"));
			request.setAttribute("email", session.getAttribute("email"));

		} else if (requestType.equals("GetGroups")) {
			request.setAttribute("email", session.getAttribute("email"));

		} else if (requestType.equals("GetChats")) {
			//request.setAttribute("email", session.getAttribute("email"));
			request.setAttribute("group", request.getParameter("group"));
		}else if (requestType.equals("GetUsers")) {
			request.setAttribute("group", request.getParameter("group"));
		}else if (requestType.equals("Login")) {
			request.setAttribute("email", request.getParameter("email"));
			request.setAttribute("password", request.getParameter("password"));

		} else if (requestType.equals("Logout")) {

		} else if (requestType.equals("Register")) {
			request.setAttribute("email", request.getParameter("email"));

		} else if (requestType.equals("Reminder")) {
			request.setAttribute("email", request.getParameter("email"));

		} else if (requestType.equals("Reset")) {

		} else if (requestType.equals("Invite")) {
			request.setAttribute("email", request.getParameter("email"));
			request.setAttribute("owner", session.getAttribute("email"));
			request.setAttribute("group", request.getParameter("group"));
			request.setAttribute("name", request.getParameter("name"));
		}  else if (requestType.equals("InviteConfirmation")) {
			request.setAttribute("email", request.getParameter("email"));
			request.setAttribute("groupID", request.getParameter("groupID"));
		} else if (requestType.equals("CreateChats")) {
			request.setAttribute("content", request.getParameter("content"));
			//request.setAttribute("email", request.getParameter("email"));
			//request.setAttribute("groupID", request.getParameter("groupID"));
		} else if (requestType.equals("DeleteUsers")) {
			request.setAttribute("groupID", request.getParameter("groupID"));
			request.setAttribute("userID", request.getParameter("userID"));
		}
		request.getRequestDispatcher(requestType).forward(request, response);

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
